﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实验2_控件遍历
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void tsbCreate_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.TV = this.treeView1;
            frm2.Show();
        }
        // 禁用结点对应控件
        private void tsbDisabled_Click(object sender, EventArgs e)
        {
            //var newObj = Convert.ChangeType(this.treeView1.SelectedNode.Tag, this.treeView1.SelectedNode.Tag.GetType());
            //this.treeView1.SelectedNode.Tag.Enabled = false;
            this.treeView1.SelectedNode.ForeColor = Color.Red;
            (this.treeView1.SelectedNode.Tag as Control).Enabled = false;
            this.treeView1.SelectedNode.Collapse(); // 折叠关闭的结点
        }
        // 启用结点对应控件
        private void tsbEnabled_Click(object sender, EventArgs e)
        {   
            // 改变当前结点的颜色
            this.treeView1.SelectedNode.ForeColor = Color.Black;
            // 当前选中结点的属性Tag指向的是与之对应的控件（赋值代码在From2.RecursiveControls()函数）
            // 将Tag指向的控件禁用
            (this.treeView1.SelectedNode.Tag as Control).Enabled = true;
        }
        // 启用或禁用控件
        private void tsbEnabledOrDisable_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.treeView1.SelectedNode.Tag.GetType().ToString());
            Control ctl = this.treeView1.SelectedNode.Tag as Control;
            ctl.Enabled = !ctl.Enabled;
            // 改变结点显示颜色
            if (ctl.Enabled)
            {
                this.treeView1.SelectedNode.ForeColor = Color.Black;
                this.treeView1.SelectedNode.Expand(); // 展开结点
            }
            else
            {
                this.treeView1.SelectedNode.ForeColor = Color.Red;
                this.treeView1.SelectedNode.Collapse(); // 折叠关闭的结点
            }
        }
    }
}
